package com.linkhub.oms.admin.modules.logistic.rules.strategy.impl;

import com.linkhub.oms.admin.modules.logistic.rules.domain.ChannelRuleMatchRequest;
import com.linkhub.oms.admin.modules.logistic.rules.strategy.ChannelRuleMatchStrategy;
import com.linkhub.oms.common.enums.IEnumConvert;
import com.linkhub.oms.common.enums.logistic.DeclaredValueLimitEnum;
import com.linkhub.oms.common.util.QtBigDecimalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Objects;

/**
 * @Description: DeclaredValueLimit
 * @Author: echo
 * @Date: 2024/6/1 15:36
 * @Version: 1.0
 */
@Slf4j
@Service
@Order(value = 1)
public class DeclaredValueLimit extends BaseChannelRuleMatchStrategy implements ChannelRuleMatchStrategy {
    @Override
    public boolean match(ChannelRuleMatchRequest request) {
        String countryCode = request.acquireOrderCountryCode();
        String currency = request.acquireOrderCurrency();
        String key = String.format("%s_%s", countryCode, currency);
        DeclaredValueLimitEnum declaredValueLimit = IEnumConvert.valueOf(key, DeclaredValueLimitEnum.class);
        if (Objects.isNull(declaredValueLimit)) {
            log.info("DeclaredValueLimit skip,  key=[{}]", key);
            return true;
        }
        BigDecimal orderTotalPrice = request.acquireOrderTotalPrice();
        // 最小清关申报金额 < 订单金额 < 最大清关申报金额
        return QtBigDecimalUtil.gt(orderTotalPrice, declaredValueLimit.getMin())
                && QtBigDecimalUtil.lt(orderTotalPrice, declaredValueLimit.getMax());

    }
}
